<template>
	<view class="chat-page">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<view>
				<uni-icons type="back" color="#00C48C" size="22" @click="navigateBack"></uni-icons>
			<image class="doctor-avatar" :src="doctorAvatar"></image>
			<text class="doctor-name">{{ doctorName }}</text>
			</view>
			
			<uni-icons type="phone-filled" color="#00C48C" size="22" @click="makeCall"></uni-icons>
		</view>

		<!-- 聊天内容区域 -->
		<scroll-view class="chat-content" scroll-y :scroll-with-animation="true" :scroll-top="scrollTop">
			<view class="message-item" v-for="(msg, index) in messages" :key="index" :class="{ 
          doctor: msg.sender === 'doctor', 
          user: msg.sender === 'user' 
        }">
				<!-- 医生消息 -->
				<template v-if="msg.sender === 'doctor'">
					<image class="avatar" :src="doctorAvatar"></image>
					<view class="bubble doctor-bubble">
						<text class="message-text">{{ msg.content }}</text>
					</view>
				</template>

				<!-- 用户消息 -->
				<template v-else>
					<view style="display: flex;">
						<view class="bubble user-bubble">
							<text class="message-text">{{ msg.content }}</text>
						</view>
						<view class="user-dot"></view>
					</view>
				</template>
			</view>
		</scroll-view>

		<!-- 底部输入栏 -->
		<view class="input-bar">
			<input class="input-box" type="text" placeholder="输入消息" v-model="inputValue" @focus="onInputFocus" />
			<button class="send-btn" @click="sendMessage">
				<uni-icons type="send" color="#FFFFFF"></uni-icons>
			</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref, nextTick } from 'vue'

	// 消息类型定义
	interface ChatMessage {
		sender : 'doctor' | 'user'
		content : string
	}

	// 状态定义
	const doctorName = ref('医生姓名')
	const doctorAvatar = ref('/static/doctor.png') // 需放置对应图片
	const messages = ref<ChatMessage[]>([
		{ sender: 'doctor', content: '您好！我是XX科医生XX，有什么可以帮到您的？' },
		{ sender: 'user', content: '我阳了' },
		{ sender: 'doctor', content: '你想听好消息还是坏消息？' },
		{ sender: 'user', content: '？？？？？？' }
	])
	const inputValue = ref('')
	const scrollTop = ref(0)

	// 导航返回
	const navigateBack = () => {
		uni.navigateBack()
	}

	// 拨打电话（模拟）
	const makeCall = () => {
		uni.showToast({ title: '拨打医生电话', icon: 'none' })
	}

	// 发送消息
	const sendMessage = () => {
		const content = inputValue.value.trim()
		if (!content) return

		messages.value.push({ sender: 'user', content })
		inputValue.value = ''

		// 滚动到底部
		nextTick(() => {
			scrollTop.value = 999999 // 触发滚动到底部
		})

		// TODO: 实际项目中添加接口请求逻辑
	}

	// 输入框聚焦处理（可选）
	const onInputFocus = () => {
		// 可添加唤起键盘后的逻辑
	}
</script>

<style scoped>
	/* 页面基础布局 */
	.chat-page {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #EBEBEB;
	}

	/* 导航栏样式 */
	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 60px;
		padding: 0 16px;
		background-color: #FFFFFF;
		border-bottom: 2px solid #00C48C;
		margin-top: 90rpx;
	}

	.doctor-avatar {
		width: 40px;
		height: 40px;
		border-radius: 50%;
		border: 2px solid #00C48C;
		margin: 0 8px;
	}

	.doctor-name {
		font-size: 18px;
		font-weight: bold;
		color: #333333;
	}

	/* 聊天内容区域 */
	.chat-content {
		flex: 1;
		padding: 16px;
		box-sizing: border-box;
	}

	/* 消息项通用样式 */
	.message-item {
		display: flex;
		margin: 12px 0;
		align-items: flex-start;
	}

	/* 头像通用样式 */
	.avatar {
		width: 36px;
		height: 36px;
		border-radius: 50%;
		margin: 6px 8px;
	}

	/* 气泡通用样式 */
	.bubble {
		padding: 10px 14px;
		border-radius: 12px;
		max-width: 60%;
		word-wrap: break-word;
		background-color: #FFFFFF;
	}

	.message-text {
		font-size: 14px;
		color: #333333;
	}

	/* 医生消息特殊样式 */
	.doctor {
		flex-direction: row;
	}

	.doctor-bubble {
		margin-left: 4px;
	}

	/* 用户消息特殊样式 */
	.user {
		flex-direction: row-reverse;
	}

	.user-bubble {
		margin-right: 4px;
	}

	.user-dot {
		width: 36px;
		height: 36px;
		border-radius: 50%;
		background-color: #00C48C;
		margin: 6px 8px;
	}

	/* 底部输入栏 */
	.input-bar {
		display: flex;
		align-items: center;
		height: 50px;
		padding: 0 12px;
		background-color: #FFFFFF;
		border-top: 1px solid #E5E5E5;
	}

	.input-box {
		flex: 1;
		height: 36px;
		padding: 0 12px;
		background-color: #F5F5F5;
		border-radius: 18px;
		font-size: 14px;
	}

	.send-btn {
		width: 36px;
		height: 36px;
		margin-left: 8px;
		background-color: #00C48C;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>